//
//  VideoTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "VideoTableView.h"
#import "VideoTableViewCell.h"
#import "MJRefresh.h"
#import "NetEaseDataService.h"
#import "UIImageView+WebCache.h"

@implementation VideoTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self addRefreshAction];
        
        [self registerClass:[VideoTableViewCell class] forCellReuseIdentifier:kVideoTableViewCellId];
    }
    
    return self;
}

//头部信息
- (void)setVideoSidListArray:(NSArray *)videoSidListArray {
    if (_videoSidListArray != videoSidListArray) {
        _videoSidListArray = [videoSidListArray copy];
        
        [self resetHeaderView];
    }
}
//重新设置头部信息
- (void)resetHeaderView {
    UIView* tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    tableHeaderView.userInteractionEnabled = YES;
    
    if (_videoSidListArray.count == 4) {
        for (int i = 0; i < 4; i++) {
            NSDictionary* dic = _videoSidListArray[i];
            NSString* imgUrl = dic[@"imgsrc"];
            NSString* title = dic[@"title"];
            
            if (!imgUrl.length || !title.length) {
                tableHeaderView = nil;
                self.tableHeaderView = nil;
                return;
            }
            
            CGFloat width = kWidth/4-40;
            //01 图标
            UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*kWidth/4+20, 15, width, width)];
            imgView.layer.cornerRadius = width/2;
            imgView.layer.masksToBounds = YES;
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
            [tableHeaderView addSubview:imgView];
            
            //02 标题
            UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(i*kWidth/4, imgView.bottom+5, kWidth/4, 20)];
            titleLabel.text = title;
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont boldSystemFontOfSize:12];
            [tableHeaderView addSubview:titleLabel];
            
            //03 透明btn
            UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(i*kWidth/4, 0, kWidth/4, 100)];
            button.tag = i+100;
            [button addTarget:self action:@selector(topButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [tableHeaderView addSubview:button];
            
            //04 细线
            UIImageView* line = [[UIImageView alloc] initWithFrame:CGRectMake(titleLabel.right, 0, 0.3, 100)];
            UIImage* lingImg = [UIImage imageNamed:@"home_channel_bar_line"];
            [lingImg stretchableImageWithLeftCapWidth:1 topCapHeight:1];
            line.image = lingImg;
            [tableHeaderView addSubview:line];
        }
        self.tableHeaderView = tableHeaderView;
        
    } else {
        tableHeaderView = nil;
        self.tableHeaderView = nil;
    }
}

- (void)topButtonAction:(UIButton*)button {
    NSLog(@"%li", button.tag-100);
}

- (void)setVideoModelArray:(NSArray *)videoModelArray {
    if (_videoModelArray != videoModelArray) {
        _videoModelArray = [videoModelArray copy];
        
        [self reloadData];
    }
}

#pragma mark - refreshAction
- (void)addRefreshAction {
    //下拉事件,刷新最新数据
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewestAction)];
    
    //上拉事件，在原基础上再刷新一波
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAction)];
}

- (void)loadNewestAction {
    [NetEaseDataService mediaVideoRequestFrom:0 size:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"视听页 视频 数据请求失败 %@", result);
            [self.header endRefreshing];
            return;
        }
        NSArray* videoListArray = result[@"videoList"];
        NSArray* videoSidListArray = result[@"videoSidList"];
        if (videoListArray.count) self.videoModelArray = videoListArray;
        if (videoSidListArray.count) self.videoSidListArray = videoSidListArray;
    }];
    
    [self.header endRefreshing];
}

- (void)loadMoreAction {
    [NetEaseDataService mediaVideoRequestFrom:self.videoModelArray.count size:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"视听页 视频 数据请求失败 %@", result);
            [self.footer endRefreshing];
            return;
        }
        NSArray* videoListArray = result[@"videoList"];
        NSArray* videoSidListArray = result[@"videoSidList"];
        if (videoListArray.count) {
            NSArray* newArray = [self.videoModelArray arrayByAddingObjectsFromArray:videoListArray];
            self.videoModelArray = newArray;
        }
        if (videoSidListArray.count) self.videoSidListArray = videoSidListArray;
    }];
    
    [self.footer endRefreshing];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _videoModelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 10)];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0.3;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kVideoTableViewCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    VideoModel* model = [[VideoModel alloc] initWithDataDic:_videoModelArray[indexPath.section]];
    cell.videoModel = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
